<?php
/**
 * test case for django template class
 */

class testDjtplTemplate extends UnitTestCase {

	function setUp() {
		// data to inject
		$this->data=array(
			'who'=>'world'
			,'list'=>array('first','second'));
		// templates parts
		$this->parts=array(
			'var'=>'<p>hello {{ who }} !</p>'
			,'for'=>'<ul>{% for item in list %}<li>{{ item }}</li>{% endfor %}</ul>'
			,'if'=>'{% if true %}<p>true is true</p>{% endif %}');
		// correct rendered results
		$this->expectations=array(
			'var'=>'<p>hello world !</p>'
			,'for'=>'<ul><li>first</li><li>second</li></ul>'
			,'if'=>'<p>true is true</p>');
		$kit=Webappkit::getkit('dj.tpl');
		$this->templates=$kit->templates;
	}

	function testVar() {
		$this->blockRoutine('var');
	}

	function testFor() {
		$this->blockRoutine('for');
	}

	function testIf() {
		$this->blockRoutine('if');
	}

	function testInclude() {
		$master=$this->templates->get('including');
		$slave=$this->templates->get('included');
		$this->assertEqual($master->render(),$slave->render()
			,"{%% include id %%} shall include template id");
		// and now with data
		$mdata=array('key'=>'some data');
		$sdata=array('include_key'=>'some data');
		$this->assertEqual($master->render($mdata),$slave->render($sdata)
			,"{%% include alias=key %%} shall set alias for inclusion %s");
	}

	/**
	 * routine for testing a template part
	 */
	function blockRoutine($name) {
		// building partial template
		$tpl=new djtplTemplate($this->parts[$name]);
		$this->assertIsA($tpl,'djtplTemplate');
		// comparing rendering with expectation
		$this->assertEqual($tpl->render($this->data),$this->expectations[$name]);
	}
}
